﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace XiaoYuStock.Core
{
    public class RequestHelp
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        public static string Get(string Url)
        {
            try
            {
                System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
                // Get the response instance.
                System.Net.WebResponse wResp = wReq.GetResponse();
                System.IO.Stream respStream = wResp.GetResponseStream();
                // Dim reader As StreamReader = New StreamReader(respStream)
                using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (System.Exception ex)
            {
                //errorMsg = ex.Message;
            }
            return "";
        }
        public static string Get(string Url,Encoding code)
        {
            try
            {
                System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
                // Get the response instance.
                System.Net.WebResponse wResp = wReq.GetResponse();
                System.IO.Stream respStream = wResp.GetResponseStream();
                // Dim reader As StreamReader = New StreamReader(respStream)
                using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, code))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (System.Exception ex)
            {
                //errorMsg = ex.Message;
            }
            return "";
        }
        public static string striphtml(string strhtml)
        {
            string stroutput = strhtml;
            Regex regex = new Regex(@"<[^>]+>|</[^>]+>");
            stroutput = regex.Replace(stroutput, "");
            return stroutput;
        }

        public static void DownLoadFile(string url,string file)
        {
            try
            {
                WebClient webClient = new WebClient();
                
                webClient.DownloadFile(url, file);
                webClient.Dispose();
            }
            catch { }
        }



        public static void DownLoadFile2(string url,string urlfile, string file)
        {
            try
            {
                CookieContainer cookies = new CookieContainer();
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.CookieContainer = cookies;
                request.Accept = "*/*";
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                response.Close();

                request = (HttpWebRequest)WebRequest.Create(urlfile);
                request.CookieContainer = cookies;
                request.Referer = url;
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)request.GetResponse();
             
                System.IO.Stream stream = myHttpWebResponse.GetResponseStream();
                StreamReader reader = new StreamReader(stream);

                byte[] buffer = new byte[32 * 1024];
                int bytesProcessed = 0;

                System.IO.FileStream fs = System.IO.File.Create(file);

                int bytesRead;
                do
                {
                    bytesRead = stream.Read(buffer, 0, buffer.Length);
                    fs.Write(buffer, 0, bytesRead);
                    bytesProcessed += bytesRead;
                }
                while (bytesRead > 0);

                fs.Flush();
                fs.Close();
                myHttpWebResponse.Close(); 
            }
            catch { }
    }

        public static string GetPostData(System.IO.Stream s)
        {
            int count = 0;
            byte[] buffer = new byte[2048];
            StringBuilder builder = new StringBuilder();
            while ((count = s.Read(buffer, 0, 2048)) > 0)
            {
                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
            }
            s.Flush();
            s.Close();
            s.Dispose();
            return builder.ToString();
        }

        public static string PostToServer(string url, string postContent)
        {
            LogHelper.Info("PostToWechatServer-------------> url: " + url + " --- post content: " + postContent);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";

            Encoding encoding = Encoding.GetEncoding("utf-8");
            if (!string.IsNullOrEmpty(postContent))
            {
                Stream stream = request.GetRequestStream();
                byte[] dataMenu = encoding.GetBytes(postContent);
                stream.Write(dataMenu, 0, dataMenu.Length);
                stream.Flush();
                stream.Close();
            }
            WebResponse response = request.GetResponse();
            Stream inStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(inStream);
            var result = sr.ReadToEnd();

            return result;
        }
    }
}